var chat;//Chat菜单
var specialOfferMsg = "让我们做得更好";
var speed = 300;
var msgud = " " + specialOfferMsg;
function titleScroll(){
    //alert(msgud.length); 
    if (msgud.length > 0) {
        msgud = msgud.substring(1, msgud.length);
        document.title = msgud.substring(0, specialOfferMsg.length);
    }
    else {
        msgud = " " + specialOfferMsg;
        document.title = msgud;
    }
    window.setTimeout("titleScroll()", speed);
}

function getSpecialOfferInfo(){
    Ext.Ajax.request({
        url: '/admin/getSpecialOffer',
        method: 'GET',
        success: function(result, request){
            //msg = result.reader.jsonData.root[0].content;
            var specialOfferMsgData = Ext.util.JSON.decode(result.responseText);
            specialOfferMsg = specialOfferMsgData.root[0].content;
        },
        failure: function(result, request){
            //Ext.MessageBox.alert('消息', "获取特价信息失败!");
        }
    });
}

Ext.apply(Ext.form.VTypes, {
    daterange: function(val, field){
        var date = field.parseDate(val);
        
        // We need to force the picker to update values to recaluate the disabled dates display
        var dispUpd = function(picker){
            var ad = picker.activeDate;
            picker.activeDate = null;
            picker.update(ad);
        };
        if (field.startDateField) {
            var sd = Ext.getCmp(field.startDateField);
            sd.maxValue = date;
            if (sd.menu && sd.menu.picker) {
                sd.menu.picker.maxDate = date;
                dispUpd(sd.menu.picker);
            }
        }
        else if (field.endDateField) {
            var ed = Ext.getCmp(field.endDateField);
            ed.minValue = date;
            if (ed.menu && ed.menu.picker) {
                ed.menu.picker.minDate = date;
                dispUpd(ed.menu.picker);
            }
        }
        /* Always return true since we're only using this vtype
         * to set the min/max allowed values (these are tested
         * for after the vtype test)
         */
        return true;
    },
    
    password: function(val, field){
        if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },
    passwordText: '密码不匹配'
});
//得到最新通信消息
function getLastMsgID(){
    Ext.Ajax.request({
        url: '/chat/getLastMsgID',
        //params: loginForm.form.getValues(true),
        method: 'GET',
        success: function(result, request){
            if (parseInt(result.responseText) > latest_chat_id) {
                latest_chat_id = parseInt(result.responseText);
                if (chatWin) {
                    if (!chatWin.isVisible()) {
                        chat.setText("内部通信<span class=red>(您有新的消息)</span>");
                    }
                }
                else {
                    chat.setText("内部通信<span class=red>(您有新的消息)</span>");
                }
            }
        },
        failure: function(result, request){
            //Ext.MessageBox.alert('提示', '内部通信错误!');
        }
    });
}

Ext.onReady(function(){
    //定期判断是不是有新的通信消息。
    setInterval("getLastMsgID()", 3000);
    Ext.QuickTips.init(); //为需要的元件提供提示信息功能
    //取特价信息
    setInterval("getSpecialOfferInfo()", 30000);
    //滚动标题
    window.setTimeout('titleScroll()', 5000);
    Ext.form.Field.prototype.msgTarget = 'side'; //设置控件的错误信息显示位置
    Ext.BLANK_IMAGE_URL = '/javascripts/ext/resources/images/default/s.gif';
    var myOrder = new Ext.Action({
        text: '我的机票订单',
        iconCls: 'album',
        handler: onItemClick
    });
    var findingOrder = new Ext.Action({
        text: '订单查找',
        handler: onItemClick,
        iconCls: 'finding'
    });
    var callersLog = new Ext.Action({
        text: '我的来电记录',
        handler: onItemClick,
        iconCls: 'mobil'
    });
    var main_business_menu = new Ext.menu.Menu({
        id: 'mainBusinessMenu',
        items: [{
            text: '来电显示',
            handler: onItemClick
        },callersLog,{
            text: '出票',
            handler: onItemClick,
            iconCls: 'yes'
        }, {
            text: '出保险',
            handler: onItemClick
        }, {
            text: '打印送票单',
            handler: onItemClick,
            iconCls: 'print'
        }, {
            text: '订单收款',
            handler: onItemClick,
            iconCls: 'gold'
        }, {
            text: '定票',
            handler: onItemClick
        }, '-', {
            text: '退出系统',
            handler: onItemClick,
            iconCls: 'exit'
        }]
    });
    
    var order_menu = new Ext.menu.Menu({
        id: 'orderMenu',
        items: [myOrder, {
            text: '我的酒店订单',
            iconCls: 'album',
            handler: onItemClick
        }, findingOrder, {
            text: '查看业绩',
            handler: onItemClick
            /*
             }, {
             text: '订单合并',
             handler: onItemClick
             */
        }, {
            text: '查看订单修改日志',
            handler: onItemClick
            /*
             }, {
             text: '查看修改日志',
             handler: onItemClick
             */
        }, {
            text: '查看接机',
            handler: onItemClick
        }, {
            text: '确认接机',
            handler: onItemClick
        }, {
            text: '提取PNR订单',
            handler: onItemClick
        }]
    });
    
    var system_menu = new Ext.menu.Menu({
        id: 'systemMenu',
        items: [{
            text: '员工管理',
            id: 'fuckyou',
            handler: onItemClick
        }, {
            text: '客户管理',
            handler: onItemClick,
            iconCls: 'customers'
        }, {
            text: '特价信息发布',
            handler: onItemClick
        }, {
            text: '舱位价格管理',
            handler: onItemClick
        }, {
            text: '转机联运管理',
            handler: onItemClick
            /*
             }, {
             text: '公布运价管理',
             handler: onItemClick
             }, {
             text: '代理费统计',
             handler: onItemClick
             }, {
             text: '送票员管理',
             handler: onItemClick
             */
        }, {
            text: '系统参数设置',
            handler: onItemClick
            /*
             }, {
             text: '短信模板设置',
             handler: onItemClick
             }, {
             text: '积分设定',
             handler: onItemClick,
             disabled: false
             }, {
             text: '合并客户资料',
             handler: onItemClick,
             disabled: false
             */
        }, {
            text: '积分维护',
            handler: onItemClick,
            disabled: false
        }, {
            text: '政策管理',
            handler: onItemClick,
            disabled: false
            /*}, {
             text: '查看消息日志',
             handler: onItemClick,
             disabled: false
             */
        }]
    });
    
    chat = new Ext.Action({
        text: '内部通信',
        handler: getChatWindow,
        iconCls: 'heart'
    });
    
    var tools_menu = new Ext.menu.Menu({
        id: 'toolsMenu',
        items: [{
            text: '客户消费统计',
            id: 'fuckyou',
            handler: onItemClick
        }, {
            text: 'VIP统计',
            handler: onItemClick
            /*
             }, {
             text: '电话查找',
             handler: onItemClick
             }, {
             text: '本票打印',
             handler: onItemClick
             }, {
             text: '黑名单管理',
             handler: onItemClick
             */
        }, {
            text: 'Q结果查看',
            handler: onItemClick
        }, chat]
    });
    
    var report_menu = new Ext.menu.Menu({
        id: 'reportMenu',
        items: [{
            text: '业务报表',
            handler: onItemClick
        }, {
            text: '电话统计',
            handler: onItemClick
        }, {
            text: '出票统计',
            handler: onItemClick
        }]
    });
    var ticket_menu = new Ext.menu.Menu({
        id: 'ticketMenu',
        items: [{
            text: '票证入库',
            id: 'fuckyou',
            handler: onItemClick
        }, {
            text: '票证分布',
            handler: onItemClick
        }, {
            text: '票证销号',
            handler: onItemClick
        }]
    });
    var finance_menu = new Ext.menu.Menu({
        id: 'ticketMenu',
        items: [{
            text: '收入支出管理',
            id: 'fuckyou',
            handler: onItemClick
        }, {
            text: '收久款',
            handler: onItemClick
        }]
    });
    var help_menu = new Ext.menu.Menu({
        id: 'helpMenu',
        items: [{
            text: '关于',
            handler: onItemClick
        }]
    });
    var tb = new Ext.Toolbar();
    tb.render('toolbar');
    
    tb.add({
        text: '业务系统',
        menu: main_business_menu // assign menu by instance
    }, {
        text: '订单管理',
        menu: order_menu // assign menu by instance
    }, {
        text: '系统管理',
        menu: system_menu // assign menu by instance
    }, {
        text: '相关工具',
        menu: tools_menu // assign menu by instance
    }, {
        text: '统计报表',
        menu: report_menu // assign menu by instance
    }, //				{
    //                    text: '票证管理',
    //                    menu: ticket_menu // assign menu by instance
    //                }, {
    //                    text: '账务管理',
    //                    menu: finance_menu // assign menu by instance
    //                }, 
    {
        text: '帮助',
        menu: help_menu // assign menu by instance
    });
    var outTicket = new Ext.Action({
        text: '出票',
        handler: onItemClick,
        iconCls: 'yes'
    });
    var printOutOrder = new Ext.Action({
        text: '打印送票单',
        handler: onItemClick,
        iconCls: 'print'
    });
    var orderGathering = new Ext.Action({
        text: '订单收款',
        handler: onItemClick,
        iconCls: 'gold'
    });
    
    tb.add('-');
    tb.add(chat);
    tb.add('-');
    tb.add(outTicket);
    tb.add('-');
    tb.add(printOutOrder);
    tb.add('-');
    tb.add(orderGathering);
    tb.add('-');
    tb.add(myOrder);
    tb.add('-');
    tb.add(findingOrder);
    tb.add('-');
    tb.add(callersLog);
    
    function onItemClick(item){
        if (item.text == "关于") {
            Ext.MessageBox.alert('关于', '制作：信长管理咨询有限公司<br><br>开发人员：Jerry,Mike');
        }
        else if (item.text == "来电显示") {
            window.document.location.href = "/index/phoneshow?phone_number=13800000000&type=new";
        }
        else if (item.text == "我的来电记录") {
            window.document.location.href = "/users/callersLogList";
        }
        else if (item.text == "我的机票订单") {
            window.document.location.href = "/orders/myOrder";
        }
        else if (item.text == "我的酒店订单") {
            window.document.location.href = "/orders/myHotelOrder";
        }
        else if (item.text == "订单查找") {
            window.document.location.href = "/orders/searchOrderMain";
        }
        else if (item.text == "员工管理") {
            window.document.location.href = "/users/userList";
        }
        else if (item.text == "系统参数设置") {
            window.document.location.href = "/admin/parameter_setup";
        }
        else if (item.text == "客户管理") {
            window.document.location.href = "/customers/customerList";
        }
        else if (item.text == "权限表维护") {
            window.document.location.href = "/rights/rightList";
        }
        else if (item.text == "客户消费统计") {
            window.document.location.href = "/customers/customerConsList";
        }
        else if (item.text == "退出系统") {
            window.document.location.href = "/index/logout";
        }
        else if (item.text == "出票") {
            window.document.location.href = "/orders/outTicketList";
        }
        else if (item.text == "出保险") {
            window.document.location.href = "/orders/outInsuranceList";
        }
        else if (item.text == "打印送票单") {
            window.document.location.href = "/orders/printSendTicketList";
        }
        else if (item.text == "内部通信") {
            window.document.location.href = "/chat/index";
        }
        else if (item.text == "业务报表") {
            window.document.location.href = "/reports/reports";
        }
        else if (item.text == "订单收款") {
            window.document.location.href = "/orders/cashCheckList";
        }
        else if (item.text == "特价信息发布") {
            window.document.location.href = "/admin/specialOffer";
        }
        else if (item.text == "舱位价格管理") {
            window.document.location.href = "/admin/positionPrice";
        }
        else if (item.text == "转机联运管理") {
            window.document.location.href = "/admin/zjlyPrice";
        }
        else if (item.text == "查看接机") {
            window.document.location.href = "/orders/searchJJ";
        }
        else if (item.text == "确认接机") {
            window.document.location.href = "/orders/checkJJ";
        }
        else if (item.text == "定票") {
            window.document.location.href = "/index/phoneshow?phone_number=0";
        }
        else if (item.text == "查看业绩") {
            window.document.location.href = "/orders/checkPerformance";
        }
        else if (item.text == "积分维护") {
            window.document.location.href = "/orders/checkPoint";
        }
        else if (item.text == "查看订单修改日志") {
            window.document.location.href = "/orders/checkOrderUpdateHis";
        }
        else if (item.text == "政策管理") {
            window.document.location.href = "/admin/policyManager";
        }
        else if (item.text == "VIP统计") {
            window.document.location.href = "/orders/expenditureList";
        }
        else if (item.text == "Q结果查看") {
            window.document.location.href = "/orders/clearQ";
        }
		else if (item.text == "提取PNR订单"){
			window.document.location.href = "/orders/rtPnr";
		}
        
        //Ext.example.msg('Menu Click', 'You clicked the "{0}" menu item.', item.text);
    }
    
    function getChatWindow(){
        chat.setText("内部通信");
        if (!chatWin) {
            chatWin = new Ext.Window({
                el: 'chat_win',
                layout: 'fit',
                width: 680,
                height: 500,
                closeAction: 'hide',
                plain: true,
                //modal: true,
                title: '内部通信',
                autoLoad: {
                    url: '/chat/index',
                    scripts: true
                },
                buttons: [{
                    text: '关闭',
                    handler: function(){
                        clearInterval(message_id);
                        chatWin.hide();
                    }
                }]
            });
        }
        else {
            var url = '/chat/index';
            chatWin.getUpdater().update(url);
        }
        chatWin.show();
    }
});
